//
//  InfoDetails.m
//  Bus
//
//  Created by wangjiajia on 13-8-5.
//  Copyright (c) 2013年 wangjiajia. All rights reserved.
//

#import "InfoDetails.h"
#import "MBProgressHUD.h"
#import "Tools.h"
#import "WebServicesUtils.h"
#import "ASIHTTPRequestDelegate.h"
#import "ASIHTTPRequest.h"
#import "NewsInfo.h"
#import "JsonParser.h"
#import "UIImage+UIImageExtras.h"
#import "AutoScrollUILabel.h"
#import "RepDetail.h"
@interface InfoDetails (){
    MBProgressHUD* hud;
    NewsInfo* newsInfo;
    RepDetail* repDetail;
    ASIHTTPRequest* globalRequest;
}

@end

@implementation InfoDetails

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated{
    
}
-(void)viewDidAppear:(BOOL)animated{
    //self.tabBarController.hidesBottomBarWhenPushed=YES;
    //self.tabBarController.tabBar.hidden=YES;

}
- (void)viewDidLoad
{
    [super viewDidLoad];
      // Do any additional setup after loading the view from its nib.
    //设置返回按钮图片
    //self.navigationItem.backBarButtonItem.title=@"xx";
    
    /*
     //变形严重
     UIBarButtonItem *back=[[UIBarButtonItem alloc] initWithTitle:@"新闻中心" style:UIBarButtonItemStyleBordered target:self action:@selector(backTo)];
     
     [back setBackgroundImage:[[UIImage imageNamed:@"brown_titlebar_back.png"] imageByScalingToSize:CGSizeMake(47.5, 27.5)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
     [back setBackgroundImage:[[UIImage imageNamed:@"brown_titlebar_back_selected.png"] imageByScalingToSize:CGSizeMake(47.5, 27.5)]forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
     */
    
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    
    [leftButton setBackgroundImage:[UIImage imageNamed:@"brown_titlebar_back.png"] forState:UIControlStateNormal];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"brown_titlebar_back_selected.png"] forState:UIControlStateHighlighted];
    leftButton.titleLabel.font=[UIFont fontWithName:@"-" size:18];
    [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [leftButton setTitle:@" 返回" forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    self.navigationItem.leftBarButtonItem = back;
    
    
    //self.navigationItem.leftItemsSupplementBackButton = NO;
    
    //self.navigationItem.hidesBackButton=YES;//隐藏返回按钮
    //设置背景
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"view_bg.png"] ]];
    //设置标题
    self.navigationItem.title=self.navigationTitle;

    if(!hud)
        hud=[[MBProgressHUD alloc] initWithView:self.view];
    [Tools showHUD:@"正在获取数据..." andView:self.view andHUD:hud];
    if (self.isDepReport) {//汇报内容
        globalRequest= [WebServicesUtils getRepDetailByID:self.newsID withDelegete:self];
    }else{
        //新闻中心 公交一天 通知公告
        globalRequest= [WebServicesUtils getNewInfoByID:self.newsID withDelegete:self];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillDisappear:(BOOL)animated{
    if (globalRequest) {
        [globalRequest clearDelegatesAndCancel];
    }
    [super viewWillDisappear:animated];
}

- (void)viewDidUnload {
    [self setNewsTitle:nil];
    [self setNewsPubInfo:nil];
    [self setNewsContent:nil];
    self.navigationTitle=nil;
    [super viewDidUnload];
}


#pragma mark- 网络处理

-(void)requestFinished:(ASIHTTPRequest *)request{
    if(hud){
        [hud hide:YES];
    }
    NSString* responseStr=[request responseString];
    if(!self.isDepReport){//新闻中心 公交一天 通知公告
        
        newsInfo=[JsonParser parseAsNewsInfo:responseStr];//如果这条数据被删除，那么返回的数据解析时就会有异常，导致应用退出
        //设置AutoScrollUILabel的宽度为字符宽度以在“一行”显示所有字，不用截取。WordWrap和CharacterWrap是多行，这里不适合。
        float wordWidth=[newsInfo.Title sizeWithFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]].width;
        float screenWidth=[UIScreen mainScreen].bounds.size.width;
        self.newsTitle.frame=CGRectMake(0, self.newsTitle.frame.origin.y, wordWidth>screenWidth?wordWidth:screenWidth, 30);//设置宽度，便于内部drawRect判断是否滚动
        //self.newsTitle.textAlignment=UITextAlignmentCenter;//IB中设置
        self.newsTitle.text=newsInfo.Title;
       
        self.newsPubInfo.text=[NSString stringWithFormat:@"发布时间:%@ 发布人:%@ 点击数:%i",newsInfo.SendTime,newsInfo.SendUserName,newsInfo.Hits];
        
        // NSString* newAritcleCont=[[newsInfo.ArticleCont stringByReplacingOccurrencesOfString:@"(?i)/WebUI/" withString:@""] stringByReplacingOccurrencesOfString:@"(?i)/ewebeditor" withString:@"ewebeditor"];
        //android下面需要去掉/webui/ 等 ios下不需要 而且支持.xls预览==>都不用
        [self.newsContent loadHTMLString:newsInfo.ArticleCont baseURL:[NSURL URLWithString:domain_url]];
        
    }else{//汇报内容
        repDetail=[JsonParser parseAsRepDetail:responseStr];
        //设置AutoScrollUILabel的宽度为字符宽度以在“一行”显示所有字，不用截取。WordWrap和CharacterWrap是多行，这里不适合
        NSString* repDepName=repDetail.RepDep;
        NSString* trimRepDepName=[repDepName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        
        NSString* repTitle=[NSString stringWithFormat:@"%@ 工作汇报",trimRepDepName];
        float wordWidth=[repTitle sizeWithFont:[UIFont fontWithName:@"Arial"size:20]].width;
        float screenWidth=[UIScreen mainScreen].bounds.size.width;
        self.newsTitle.frame=CGRectMake(0, self.newsTitle.frame.origin.y, wordWidth>screenWidth?wordWidth:screenWidth, 30);
        //self.newsTitle.textAlignment=UITextAlignmentCenter;//IB中设置
        self.newsTitle.text=repTitle;
        
        self.newsPubInfo.text=[NSString stringWithFormat:@"汇报时间:%@",repDetail.RealTime];
        
        [self.newsContent loadHTMLString:repDetail.ArticleCont baseURL:[NSURL URLWithString:domain_url]];
    }
    //NSLog(@"%@",NSStringFromCGRect(self.newsContent.frame));
}

-(void)requestFailed:(ASIHTTPRequest *)request{
    
    if(hud){
        [hud hide:YES];
    }
    
    [Tools toastNotification:@"处理请求失败，请检查网络连接!" andView:self.view andLoading:NO andIsBottom:NO];
}

#pragma mark-- 返回到上一个视图
-(void)backTo{
    if(self.newsContent.canGoBack){
        [self.newsContent goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
